<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI绘画生成器</title>
    <style>
        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f0f2f5;
            color: #333;
        }
        
        .container {
            display: flex;
            gap: 30px;
            background-color: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }
        
        .control-panel {
            flex: 1;
            max-width: 500px;
        }
        
        .result-panel {
            flex: 1;
            min-width: 0;
            display: flex;
            flex-direction: column;
            border-left: 1px solid #eee;
            padding-left: 30px;
        }
        
        @media (max-width: 900px) {
            .container {
                flex-direction: column;
            }
            
            .control-panel {
                max-width: 100%;
            }
            
            .result-panel {
                border-left: none;
                border-top: 1px solid #eee;
                padding-left: 0;
                padding-top: 30px;
                margin-top: 30px;
            }
        }
        
        .result-panel h2 {
            margin-top: 0;
            color: #1a1a1a;
            font-size: 20px;
            font-weight: 500;
        }
        
        .preview-area {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #f8f9fa;
            border-radius: 8px;
            min-height: 300px;
            margin: 20px 0;
        }
        
        .preview-area.empty {
            border: 2px dashed #ddd;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #2c3e50;
            font-size: 15px;
        }
        
        textarea {
            width: 100%;
            padding: 12px;
            border: 2px solid #e8e8e8;
            border-radius: 8px;
            box-sizing: border-box;
            font-size: 14px;
            transition: border-color 0.3s ease;
            resize: vertical;
            min-height: 100px;
        }
        
        textarea:focus {
            border-color: #4CAF50;
            outline: none;
        }
        
        select {
            width: 100%;
            padding: 12px;
            border: 2px solid #e8e8e8;
            border-radius: 8px;
            background-color: white;
            font-size: 14px;
            cursor: pointer;
            appearance: none;
            -webkit-appearance: none;
            background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
            background-repeat: no-repeat;
            background-position: right 12px center;
            background-size: 16px;
            transition: border-color 0.3s ease;
        }
        
        select:focus {
            border-color: #4CAF50;
            outline: none;
        }
        
        button {
            background-color: #4CAF50;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            width: 100%;
            transition: background-color 0.3s ease;
        }
        
        button:hover {
            background-color: #45a049;
        }
        
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        
        #generatedImage {
            max-width: 100%;
            max-height: 600px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            object-fit: contain;
        }
        
        .loading {
            display: none;
            margin: 20px 0;
            text-align: center;
            color: #666;
            font-size: 14px;
        }
        
        @keyframes pulse {
            0% { opacity: 0.6; }
            50% { opacity: 1; }
            100% { opacity: 0.6; }
        }
        
        .loading {
            animation: pulse 1.5s infinite;
        }
        
        a {
            color: #4CAF50;
            text-decoration: none;
            font-weight: 500;
        }
        
        a:hover {
            text-decoration: underline;
        }
        
        .error {
            color: #dc3545;
            padding: 10px;
            border-radius: 6px;
            background-color: #fff5f5;
            margin-top: 10px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="control-panel">
            <h1>AI绘画生成器</h1>
            <div class="form-group">
                <label for="prompt">描述你想要的图片：</label>
                <textarea id="prompt" rows="4" placeholder="例如：一只可爱的猫咪在花园里玩耍"></textarea>
            </div>
            <div class="form-group">
                <label for="size">选择图片尺寸：</label>
                <select id="size">
                    <option value="1024*1024">1024x1024</option>
                    <option value="720*1280">720x1280</option>
                    <option value="1280*720">1280x720</option>
                    <option value="768*1152">768x1152</option>
                </select>
            </div>
            <div class="form-group">
                <label for="style">选择图片风格：</label>
                <select id="style">
                    <option value="auto">默认</option>
                    <option value="photography">摄影</option>
                    <option value="portrait">人像写真</option>
                    <option value="3d cartoon">3D卡通</option>
                    <option value="anime">动画</option>
                    <option value="oil painting">油画</option>
                    <option value="watercolor">水彩</option>
                    <option value="sketch">素描</option>
                    <option value="chinese painting">中国画</option>
                    <option value="flat illustration">扁平插画</option>
                </select>
            </div>
            <button onclick="generateImage()" id="generateBtn">生成图片</button>
            <div class="loading" id="loading">
                正在生成图片，请稍候...
            </div>
        </div>
        
        <div class="result-panel">
            <h2>生成结果</h2>
            <div class="preview-area empty" id="result">
                <p style="color: #666;">生成的图片将在这里显示</p>
            </div>
        </div>
    </div>

    <script>
        async function generateImage() {
            const prompt = document.getElementById('prompt').value;
            const size = document.getElementById('size').value;
            const style = document.getElementById('style').value;
            const generateBtn = document.getElementById('generateBtn');
            const loading = document.getElementById('loading');
            const result = document.getElementById('result');

            if (!prompt) {
                alert('请输入图片描述');
                return;
            }

            // 禁用按钮并显示加载提示
            generateBtn.disabled = true;
            loading.style.display = 'block';
            result.innerHTML = '<p style="color: #666;">正在生成图片...</p>';
            result.classList.add('empty');

            try {
                const response = await fetch('/generate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        prompt: prompt,
                        size: size,
                        style: style
                    })
                });

                const data = await response.json();

                if (data.status === 'success') {
                    result.classList.remove('empty');
                    result.innerHTML = `
                        <div style="text-align: center;">
                            <img id="generatedImage" src="${data.download_url}" alt="生成的图片">
                            <p style="margin-top: 15px;">
                                <a href="${data.download_url}" download>下载图片</a>
                            </p>
                        </div>
                    `;
                } else {
                    result.innerHTML = `<p class="error">错误：${data.message}</p>`;
                }
            } catch (error) {
                result.innerHTML = `<p class="error">生成失败：${error.message}</p>`;
            } finally {
                generateBtn.disabled = false;
                loading.style.display = 'none';
            }
        }
    </script>
</body>
</html> 